package com.javaweb.blogcite.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.javaweb.blogcite.mapper.SettingMapper;
import com.javaweb.blogcite.pojo.Setting;
import com.javaweb.blogcite.service.SettingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class SettingServiceImpl implements SettingService {
    @Resource
    private SettingMapper settingMapper;

    @Override
    public Setting listAllSettings() {
        return settingMapper.selectList(new QueryWrapper<>()).get(0);
    }

    @Override
    public boolean updateSetting(List<Setting> settingList) {
        int rows = 0;
        for(Setting setting : settingList) {
            UpdateWrapper<Setting> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", setting.getId())
                    .set("avatar", setting.getAvatar())
                    .set("site_title", setting.getSiteTitle())
                    .set("favicon", setting.getFavicon())
                    .set("github", setting.getGithub())
                    .set("github_name", setting.getGithubName())
                    .set("icp", setting.getIcp())
                    .set("mail", setting.getMail())
                    .set("qq", setting.getQq())
                    .set("qq_qr_code",setting.getQqQrCode())
                    .set("weixin", setting.getWeixin())
                    .set("weixin_qr_code",setting.getWeixinQrCode());
            rows += settingMapper.update(null, updateWrapper);
        }
        return rows > 0;
    }
}
